package com.shiku.delayjob.consumer;

import com.shiku.delayjob.handler.WaitDelayJobHanler;
import com.shiku.delayjob.model.WaitDelayJob;
import com.shiku.delayjob.producer.ISubmitJobService;
import com.shiku.delayjob.repository.WaitDelayJobDO;
import com.shiku.delayjob.repository.WaitDelayJobRepository;
import com.shiku.mongodb.utils.BeanCopyUtils;
import com.shiku.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhm
 * @version V1.0
 * @Description: TODO(todo)
 * @date 2020/1/13 19:12
 */
@Service
public class AbstractWaitDelayBucketHandler implements WaitDelayJobHanler {

    @Autowired
    private WaitDelayJobRepository waitDelayJobRepository;

    @Autowired
    private ISubmitJobService submitJobService;

//    @Autowired
//    private IPushWaitDealyJob iPushWaitDealyJob;
    @Autowired
    private IExecuteDealyJob iExecuteDealyJob;

    @Override
    public void pullWaitDelayJob() {
        List<WaitDelayJobDO> list =waitDelayJobRepository.queryListDelayJobByWaitEndTime(DateUtil.currentTimeSeconds()+120);
        for(WaitDelayJobDO waitDelayJobDO : list){
            WaitDelayJob waitDelayJob = new WaitDelayJob();
            BeanCopyUtils.copyProperties(waitDelayJobDO,waitDelayJob);
            // 等待时间小于两个小时的
            if(waitDelayJob.getWaitEndTime() - DateUtil.currentTimeSeconds() <= 7200){
                submitJobService.submitJob(waitDelayJob,waitDelayJob.getExecTime());
                waitDelayJobRepository.deleteDelayJob(waitDelayJob.getJobId());
            }
        }
    }
}
